/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import DAO.DanhMucDAO;
import DAO.LoaiSanPhamDAO;
import DAO.NguoiDungDAO;
import DAO.SanPhamDAO;
import POJO.DanhMucPOJO;
import POJO.LoaiSanPhamPOJO;
import POJO.NguoiDungPOJO;
import POJO.SanPhamPOJO;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

/**
 *
 * @author NGUYEN CONG BINH
 */
@WebServlet(name = "RegisterController", urlPatterns = {"/RegisterController"})
public class RegisterController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("Action");
        if (action != null) {
            //bảng danh muc
            ArrayList<DanhMucPOJO> dm = DanhMucDAO.layDanhSachDanhMuc();
            request.setAttribute("danhmucAttribute", dm);
            //bảng loại sản phẩm
            ArrayList<LoaiSanPhamPOJO> loaisanpham = LoaiSanPhamDAO.layDanhSachLoaiSanPham();
            request.setAttribute("loaisanphamAttribute", loaisanpham);
            //bảng sản phẩm
            ArrayList<SanPhamPOJO> sanpham = SanPhamDAO.layDanhSachSanPham();
            request.setAttribute("sanphamAttribute", sanpham);
            if (action.equals("View")) {

                request.getRequestDispatcher("/Register.jsp").forward(request, response);
            }
            if (action.equals("Register")) {
                //<editor-fold defaultstate="collapsed" desc="comment">


                String your_public_key = "6LdRqMkSAAAAAPwajDjWuUDmFOE7RiW9GRU7DDbA";
                String your_private_key = "6LdRqMkSAAAAAF5sYB7KBZV8KCNBl5rSKxIjLLTA";
                String remoteAddr = request.getRemoteAddr();
                ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
                reCaptcha.setPrivateKey(your_private_key);

                String challenge = request.getParameter("recaptcha_challenge_field");
                String uresponse = request.getParameter("recaptcha_response_field");
                ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
                //</editor-fold>
                if (reCaptchaResponse.isValid()) {

                    NguoiDungPOJO kh = new NguoiDungPOJO();
                    String matKhau = "123";
                    if (request.getParameter("tb_matkhau") != null) {
                        matKhau = NguoiDungDAO.encryptPass(request.getParameter("tb_matkhau"));
                        kh.setMatKhau(matKhau);
                    }
                    if (request.getParameter("tb_tendangnhap") != null) {
                        kh.setTenDangNhap(request.getParameter("tb_tendangnhap"));
                    }
                    if (request.getParameter("tb_hovaten") != null) {
                        kh.setHoTenNguoiDung(request.getParameter("tb_hovaten"));
                    }
                    if (request.getParameter("tb_sodienthoai") != null) {
                        kh.setDienThoai(Integer.parseInt(request.getParameter("tb_sodienthoai")));
                    }
                    if (request.getParameter("tb_diachi") != null) {
                        kh.setDiaChi(request.getParameter("tb_diachi"));
                    }
                    if (request.getParameter("tb_email") != null) {
                        kh.seteMail(request.getParameter("tb_email"));
                    }
                    kh.setMaCapBac(3);

                    boolean kq = false;
                    kq = NguoiDungDAO.dangky(kh);

                    if (kq == true) {
                        request.getRequestDispatcher("/Register_Succeed.jsp").forward(request, response);
                    } else {
                        String url = "RegisterController?Action=View";
                        request.setAttribute("Error", "Chưa điền đầy đủ thông tin! ");
                        request.getRequestDispatcher(url).forward(request, response);
                    }
                } else {

                    String url = "RegisterController?Action=View";
                    request.setAttribute("Error", "Captcha nhập sai, Xin vui lòng nhập lại ");
                    request.getRequestDispatcher(url).forward(request, response);

                }

            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
